﻿#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Web;
using facebook;
using facebook.Utility;

namespace raya.Library
{
    /// <summary>
    /// The beginning of a .NET Facebook client as per PHP examples
    /// </summary>
public class Facebook
    {
        public readonly API api;

        public Facebook()
        {
            api = new API
            {
                ApplicationKey = ApiKey,
                SessionKey = SessionKey,
                Secret = SecretKey,
                uid = UserID
            };
        }

        public bool isConnected()
        {
            if (api.SessionKey == null)
            {
                return false;
            }
            // can't really tell if the session key we have is still valid
            // let's try connecting
            try
            {
                api.uid = api.users.getLoggedInUser();
            }
            catch (FacebookException)
            {
                // invalid session key found
                ClearFacebookCookies();
                return false;
            }
            return true;
        }

        public static string ApiKey
        {
            get
            {
                return RayaSettings.Fb_api_key;
            }
        }

        public static string SessionKey
        {
            get
            {
                return GetFacebookCookie("session_key");
            }
        }
        public static string SecretKey
        {
            get
            {
                return RayaSettings.Fb_api_secret;
            }
        }
        public static long UserID
        {
            get
            {
                int userID;
                int.TryParse(GetFacebookCookie("user"), out userID);
                return userID;
            }
        }
        private static string GetFacebookCookie(string cookieName)
        {
            string retString = null;
            string fullCookie = ApiKey + "_" + cookieName;

            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Request.Cookies[fullCookie] != null)
                    retString = HttpContext.Current.Request.Cookies[fullCookie].Value;
            }

            return retString;
        }

        public static void ClearFacebookCookies()
        {
            string[] cookies = new[]{"user", "session_key", "expires", "ss"};
            foreach (var c in cookies)
            {
                string fullCookie = ApiKey + "_" + c;

                if (HttpContext.Current != null && 
                    HttpContext.Current.Response.Cookies[fullCookie] != null)
                {
                    HttpContext.Current.Response.Cookies[fullCookie].Expires = DateTime.Now.AddMonths(-1);
                }
            }
        }
    }
}